/*
 * Game Object: Player
 */

package demo;

import java.awt.Graphics;
import javaPlay.GameObject;
import javaPlay.Sprite;

/**
 * @author VisionLab/PUC-Rio
 */
public class Player extends GameObject {
	
	private Sprite sprite;
	
	private int currFrame;
	
	/*
	 * animation with 8 frames - file "tankplayer.png" private final int[] UP_FRAMES = {0, 1}; private final int[] LEFT_FRAMES = {2, 3}; private final int[] RIGHT_FRAMES = {6, 7}; private final int[] DOWN_FRAMES = {4, 5}; private final int SHOW_STEP = 1;
	 */
	
	/* animation with 40 frames - file "walkingplayer.png" */
	private final int[] UP_FRAMES = {
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9
	};
	
	private final int[] LEFT_FRAMES = {
	10, 11, 12, 13, 14, 15, 16, 17, 18, 19
	};
	
	private final int[] RIGHT_FRAMES = {
	20, 21, 22, 23, 24, 25, 26, 27, 28, 29
	};
	
	private final int[] DOWN_FRAMES = {
	30, 31, 32, 33, 34, 35, 36, 37, 38, 39
	};
	
	private final int SHOW_STEP = 30;
	
	private int animIndex;
	
	private int currMoveAnim;
	
	private int stepCounter;
	
	private final int MOVE_ANIM_UP = 0;
	
	private final int MOVE_ANIM_DOWN = 1;
	
	private final int MOVE_ANIM_LEFT = 2;
	
	private final int MOVE_ANIM_RIGHT = 3;
	
	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
		animIndex = 0;
		currFrame = UP_FRAMES[animIndex];
		currMoveAnim = MOVE_ANIM_UP;
		stepCounter = 0;
	}
	
	public void moveUp() {
		y--;
		currMoveAnim = MOVE_ANIM_UP;
	}
	
	public void moveDown() {
		y++;
		currMoveAnim = MOVE_ANIM_DOWN;
	}
	
	public void moveLeft() {
		x--;
		currMoveAnim = MOVE_ANIM_LEFT;
	}
	
	public void moveRight() {
		x++;
		currMoveAnim = MOVE_ANIM_RIGHT;
	}
	
	public void step(long timeElapsed) {
		switch (currMoveAnim) {
		case MOVE_ANIM_UP:
			if (++stepCounter >= SHOW_STEP) {
				animIndex = (animIndex + 1) % UP_FRAMES.length;
				currFrame = UP_FRAMES[animIndex];
				stepCounter = 0;
			}
			break;
		case MOVE_ANIM_DOWN:
			if (++stepCounter >= SHOW_STEP) {
				animIndex = (animIndex + 1) % DOWN_FRAMES.length;
				currFrame = DOWN_FRAMES[animIndex];
				stepCounter = 0;
			}
			break;
		case MOVE_ANIM_LEFT:
			if (++stepCounter >= SHOW_STEP) {
				animIndex = (animIndex + 1) % LEFT_FRAMES.length;
				currFrame = LEFT_FRAMES[animIndex];
				stepCounter = 0;
			}
			break;
		case MOVE_ANIM_RIGHT:
			if (++stepCounter >= SHOW_STEP) {
				animIndex = (animIndex + 1) % RIGHT_FRAMES.length;
				currFrame = RIGHT_FRAMES[animIndex];
				stepCounter = 0;
			}
			break;
		}
	}
	
	public void draw(Graphics g) {
		sprite.setCurrentAnimationFrame(currFrame);
		sprite.draw(g, x, y);
	}
}
